<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class References extends CI_Controller {

	public function index() {

	}

	public function clusters() {
		$result = array();

		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$this->load->model('clusters');
			$result = $this->clusters->getAll();
		}

		echo json_encode($result);
	}

	public function countries() {
		$result = array();

		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$this->load->model('countries');
			$result = $this->countries->getAll();
		}

		echo json_encode($result);
	}

	public function districts() {
		$result = array();

		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$this->load->model('districts');
			$result = $this->districts->getAll();
		}

		echo json_encode($result);
	}

	public function educationalDegrees() {
		$result = array();

		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$this->load->model('educationaldegrees');
			$result = $this->educationaldegrees->getAll();
		}

		echo json_encode($result);	
	}

	public function localities() {
		$region = $this->input->post('region');

		$result = array();

		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$this->load->model('localities');
			if (trim($region) != '') $result = $this->localities->getAll($region);
			else $result = $this->localities->getEverything();
		}

		echo json_encode($result);
	}

	public function localityNames() {
		$region = $this->input->post('region');

		$data = array();

		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$this->load->model('localities');
			if (trim($region) != '') $result = $this->localities->getAll($region);
			else $result = $this->localities->getEverything();

			foreach ($result as $row) {
				array_push($data, $row['Locality']);
			}
		}
		
		echo json_encode($data);
	}

	public function localityGroups() {
		$result = array();

		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$this->load->model('localitygroups');
			$result = $this->localitygroups->getAll();
		}

		echo json_encode($result);
	}

	public function localityGroupNames() {
		$data = array();
		
		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$this->load->model('localitygroups');
			$result = $this->localitygroups->getAll();
				
			foreach ($result as $row) {
				array_push($data, $row['LocalityGroup']);
			}
		}

		echo json_encode($data);
	}

	public function paginatedTrainees() {
		$page = $this->input->post('page');
		$rows = $this->input->post('rows');

		$result = array();

		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$this->load->model('trainees');
			$result = $this->trainees->getPaginatedList($page, $rows);
		}
		
		echo json_encode($result);
	}

	public function regionGroupNames() {
		$data = array();

		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$this->load->model('regiongroups');
			$result = $this->regiongroups->getAll();
			
			foreach ($result as $row) {
				array_push($data, $row['RegionGroup']);	
			}
		}
		
		echo json_encode($data);
	}

	public function regionNames() {
		$data = array();

		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$this->load->model('regions');
			$result = $this->regions->getAll();

			foreach ($result as $row) {
				array_push($data, $row['Region']);	
			}
		}

		echo json_encode($data);
	}

	public function regionGroups() {
		$result = array();

		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$this->load->model('regiongroups');
			$result = $this->regiongroups->getAll();
		}

		echo json_encode($result);
	}

	public function regions() {
		$result = array();

		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$this->load->model('regions');
			$result = $this->regions->getAll();
		}
		
		echo json_encode($result);
	}

	public function search() {
		$result = array();

		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$search = $this->input->post('search');

			$this->load->model('trainees');
			$result = $this->trainees->search($search);
		}
		
		echo json_encode($result);
	}

	public function trainee() {
		$id = $this->input->post('id');

		$result = array();

		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$this->load->model('trainees');
			$result = $this->trainees->getIndividualInfo($id);
		}
		
		echo json_encode($result);
	}

	public function trainees() {
		$result = array();

		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$this->load->model('trainees');
			$result = $this->trainees->getAll();
		}
		
		echo json_encode($result);	
	}

	public function trainingLevels() {
		$result = array();

		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$this->load->model('traininglevels');
			$result = $this->traininglevels->getAll();
		}
		
		echo json_encode($result);	
	}
	
	public function user() {
		$id = $this->input->post('id');

		$result = array();

		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$this->load->model('users');
			$result = $this->users->getIndividualUser($id);
		}
		
		echo json_encode($result);
		
	}
	
	public function users() {
		$result = array();
		
		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$this->load->model('users');
			$result = $this->users->getAll();
		}
		
		echo json_encode($result);
	}
	
	public function update() {
		$data = $this->input->post('data');

		if (!isset($_SESSION)) session_start();
		
		$view = '';
		if (isset($_SESSION[CURRENT_PAGE_SESSION]))  $view = trim(strtolower($_SESSION[CURRENT_PAGE_SESSION]));

		$saved = false;
		$viewUpdates = array();
		$updates = json_decode($data);
		$message = '';

		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			if ($view == 'region-groups') {
				$proceed = true;

				$this->load->model('regiongroups');
				
				foreach ($updates as $update) {
					if ($update->RowState == 'added') {
						if ($this->regiongroups->exists($update->RegionGroup)) {
							$proceed = false;
							$message = $update->RegionGroup . ' already exists.';
							break;
						}
					}
					else if ($update->RowState == 'modified') {
						$id = $this->regiongroups->getId($update->RegionGroup);
						if ($id > 0) {
							if ($id != $update->Id) {
								$proceed = false;
								$message = $update->RegionGroup . ' already exists.';
								break;		
							}
						}
					}
					else {

					}
				}

				if ($proceed) {
					$saved = $this->regiongroups->save($updates);
				
					if ($saved) {
						foreach ($updates as $update) {
							if ($update->RowState == 'added') {
								$id = $this->regiongroups->getId($update->RegionGroup);
								array_push($viewUpdates, array(
															'Id' => $update->Id,
															'GeneratedId' => $id,
															'RegionGroup' => $update->RegionGroup,
															'Description' => $update->Description
														));
							}
						}
					}
				}	
			}
			else if ($view == 'regions') {
				$proceed = true;

				$this->load->model('regions');

				foreach ($updates as $update) {
					if ($update->RowState == 'added') {
						if ($this->regions->exists($update->RegionGroup, $update->Region)) {
							$proceed = false;
							$message = $update->Region . ' already exists.';
							break;
						}
					}
					else if ($update->RowState == 'modified') {
						$id = $this->regions->getId($update->RegionGroup, $update->Region);
						if ($id > 0) {
							if ($id != $update->Id) {
								$proceed = false;
								$message = $update->Region . ' already exists.';
								break;		
							}
						}
					}
					else {

					}
				}

				if ($proceed) {
					$saved = $this->regions->save($updates);
				
					if ($saved) {
						foreach ($updates as $update) {
							if ($update->RowState == 'added') {
								$id = $this->regions->getId($update->RegionGroup, $update->Region);
								array_push($viewUpdates, array(
															'Id' => $update->Id,
															'GeneratedId' => $id,
															'RegionGroup' => $update->RegionGroup,
															'Region' => $update->Region,
															'Description' => $update->Description
														));
							}
						}
					}
				}
			}
			else if ($view == 'locality-groups') {
				$proceed = true;

				$this->load->model('localitygroups');

				foreach ($updates as $update) {
					if ($update->RowState == 'added') {
						if ($this->localitygroups->exists($update->Region, $update->LocalityGroup)) {
							$proceed = false;
							$message = $update->LocalityGroup . ' already exists.';
							break;
						}
					}
					else if ($update->RowState == 'modified') {
						$id = $this->localitygroups->getId($update->Region, $update->LocalityGroup);
						if ($id > 0) {
							if ($id != $update->Id) {
								$proceed = false;
								$message = $update->LocalityGroup . ' already exists.';
								break;		
							}
						}
					}
					else {

					}
				}

				if ($proceed) {
					$saved = $this->localitygroups->save($updates);
				
					if ($saved) {
						foreach ($updates as $update) {
							if ($update->RowState == 'added') {
								$id = $this->localitygroups->getId($update->Region, $update->LocalityGroup);
								array_push($viewUpdates, array(
															'Id' => $update->Id,
															'GeneratedId' => $id,
															'Region' => $update->Region,
															'LocalityGroup' => $update->LocalityGroup,
															'Description' => $update->Description
														));
							}
						}
					}
				}
			}
			else if ($view == 'localities') {
				$proceed = true;

				$this->load->model('localities');

				foreach ($updates as $update) {
					if ($update->RowState == 'added') {
						if ($this->localities->exists($update->Region, $update->Locality)) {
							$proceed = false;
							$message = $update->Locality . ' already exists.';
							break;
						}
					}
					else if ($update->RowState == 'modified') {
						$id = $this->localities->getId($update->Region, $update->Locality);
						if ($id > 0) {
							if ($id != $update->Id) {
								$proceed = false;
								$message = $update->Locality . ' already exists.';
								break;		
							}
						}
					}
					else {

					}
				}

				if ($proceed) {
					$saved = $this->localities->save($updates);
				
					if ($saved) {
						foreach ($updates as $update) {
							if ($update->RowState == 'added') {
								$id = $this->localities->getId($update->Region, $update->Locality);
								array_push($viewUpdates, array(
															'Id' => $update->Id,
															'GeneratedId' => $id,
															'Region' => $update->Region,
															'LocalityGroup' => $update->LocalityGroup,
															'Locality' => $update->Locality,
															'LocalityType' => $update->LocalityType,
															'Description' => $update->Description
														));
							}
						}
					}
				}
			}
			else if ($view == 'clusters') {
				$proceed = true;

				$this->load->model('clusters');

				foreach ($updates as $update) {
					if ($update->RowState == 'added') {
						if ($this->clusters->exists($update->Locality, $update->Cluster)) {
							$proceed = false;
							$message = $update->Cluster . ' already exists.';
							break;
						}
					}
					else if ($update->RowState == 'modified') {
						$id = $this->clusters->getId($update->Locality, $update->Cluster);
						if ($id > 0) {
							if ($id != $update->Id) {
								$proceed = false;
								$message = $update->Cluster . ' already exists.';
								break;		
							}
						}
					}
					else {

					}
				}

				if ($proceed) {
					$saved = $this->clusters->save($updates);
				
					if ($saved) {
						foreach ($updates as $update) {
							if ($update->RowState == 'added') {
								$id = $this->clusters->getId($update->Locality, $update->Cluster);
								array_push($viewUpdates, array(
															'Id' => $update->Id,
															'GeneratedId' => $id,
															'Locality' => $update->Locality,
															'Cluster' => $update->Cluster,
															'Description' => $update->Description
														));
							}
						}
					}
				}
			}
			else if ($view == 'districts') {
				$proceed = true;

				$this->load->model('districts');

				foreach ($updates as $update) {
					if ($update->RowState == 'added') {
						if ($this->districts->exists($update->Locality, $update->District)) {
							$proceed = false;
							$message = $update->District . ' already exists.';
							break;
						}
					}
					else if ($update->RowState == 'modified') {
						$id = $this->districts->getId($update->Locality, $update->District);
						if ($id > 0) {
							if ($id != $update->Id) {
								$proceed = false;
								$message = $update->District . ' already exists.';
								break;		
							}
						}
					}
					else {

					}
				}

				if ($proceed) {
					$saved = $this->districts->save($updates);
				
					if ($saved) {
						foreach ($updates as $update) {
							if ($update->RowState == 'added') {
								$id = $this->districts->getId($update->Locality, $update->District);
								array_push($viewUpdates, array(
															'Id' => $update->Id,
															'GeneratedId' => $id,
															'Locality' => $update->Locality,
															'Cluster' => $update->Cluster,
															'District' => $update->District,
															'Description' => $update->Description
														));
							}
						}
					}
				}
			}
			else if ($view == 'educational-degrees') {
				$proceed = true;

				$this->load->model('educationaldegrees');

				foreach ($updates as $update) {
					if ($update->RowState == 'added') {
						if ($this->educationaldegrees->exists($update->EducationalDegree)) {
							$proceed = false;
							$message = $update->EducationalDegree . ' already exists.';
							break;
						}
					}
					else if ($update->RowState == 'modified') {
						$id = $this->educationaldegrees->getId($update->EducationalDegree);
						if ($id > 0) {
							if ($id != $update->Id) {
								$proceed = false;
								$message = $update->EducationalDegree . ' already exists.';
								break;		
							}
						}
					}
					else {

					}
				}

				if ($proceed) {
					$saved = $this->educationaldegrees->save($updates);
				
					if ($saved) {
						foreach ($updates as $update) {
							if ($update->RowState == 'added') {
								$id = $this->educationaldegrees->getId($update->EducationalDegree);
								array_push($viewUpdates, array(
															'Id' => $update->Id,
															'GeneratedId' => $id,
															'EducationalDegree' => $update->EducationalDegree
														));
							}
						}
					}
				}
			}
			else if ($view == 'training-levels') {
				$proceed = true;

				$this->load->model('traininglevels');

				foreach ($updates as $update) {
					if ($update->RowState == 'added') {
						if ($this->traininglevels->exists($update->TrainingLevel)) {
							$proceed = false;
							$message = $update->TrainingLevel . ' already exists.';
							break;
						}
					}
					else if ($update->RowState == 'modified') {
						$id = $this->traininglevels->getId($update->TrainingLevel);
						if ($id > 0) {
							if ($id != $update->Id) {
								$proceed = false;
								$message = $update->TrainingLevel . ' already exists.';
								break;		
							}
						}
					}
					else {

					}
				}

				if ($proceed) {
					$saved = $this->traininglevels->save($updates);
				
					if ($saved) {
						foreach ($updates as $update) {
							if ($update->RowState == 'added') {
								$id = $this->traininglevels->getId($update->TrainingLevel);
								array_push($viewUpdates, array(
															'Id' => $update->Id,
															'GeneratedId' => $id,
															'TrainingLevel' => $update->TrainingLevel,
															'Description' => $update->Description
														));
							}
						}
					}
				}
			}
			else if ($view == 'trainee-info' ||
					 $view == 'trainees') {
				if (isset($updates->trainee)) {
					$trainee = $updates->trainee;

					$this->load->model('trainees');
					
					if (!$this->trainees->emailExists($trainee->Email, $trainee->Id)) {
						$proceed = true;

						if (trim($trainee->Username) != '') {
							$this->load->model('users');
							$proceed = (!$this->users->traineeExists($trainee->Username, $trainee->Id));
						}

						if ($proceed) {
							$viewUpdates = $this->trainees->save($updates);
							$saved = $viewUpdates['saved'];
						}
						else {
							$message = 'Username has been already used by another application user.';	
						}
					}
					else {
						$message = 'Email address has been already used by another trainee.';	
					}
				}
				else {
					$message = 'Unable to determine trainee information.';
				}
			}
			else {

			}
		}
		else {
			$message = 'Unauthorized action.';
		}

		$result = array( 'saved' => $saved, 'viewUpdates' => $viewUpdates, 'message' => $message);
		
		echo json_encode($result);
	}

	public function updateProfilePict() {
		$data = $this->input->post('data');

		if (!isset($_SESSION)) session_start();
		
		$saved = false;
		$updates = json_decode($data);
		$message = '';

		if (!isset($_SESSION)) session_start();
		if (isset($_SESSION[CURRENT_USER_SESSION])) {
			$traineeId = $updates->Id;
			$image = $updates->Image;

			$this->load->model('trainees');
			$saved = $this->trainees->saveImage($traineeId, $image);
		}
		else {
			$message = 'Unauthorized action.';
		}

		$result = array( 'saved' => $saved, 'message' => $message);

		echo json_encode($result);
	}

}